package org.example;
import com.alibaba.fastjson.JSON;
import org.apache.http.client.HttpClient;
import net.sf.json.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import java.io.*;

import java.nio.charset.StandardCharsets;

import org.apache.http.HttpResponse;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击间距中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Stop_Serve {
    public static void main(String[] args) throws IOException {
        String instance_id = System.getenv("JM_INSTANCE_ID");
        String taskRef = System.getenv("JM_TASK_REF");
        String token = System.getenv("JIANMU_TOKEN");
        String url = System.getenv("JIANMU_URL");
        System.out.println(url);
        JSONObject jsob1 = new JSONObject();
        jsob1.put("instanceId",instance_id);
        jsob1.put("taskRef",taskRef);
        jsob1.put("token",token);
        jsob1.put("url",url);
//        jsob1.put("instanceId","instance_id");
//        jsob1.put("taskRef","taskRef");
//        jsob1.put("token","token");
        //post(jsob1,"http://192.168.3.30:8080/getmassage/authentication");
        post(jsob1,"http://"+url+":8080/getmassage/authentication");

        //post(token,"http://192.168.3.30:8080/getmassage/authentication");
        //put("http://192.168.3.30:8080/workflow_instances/stop/","instanceId",Token.getToken());
        put("http://"+url+"/workflow_instances/stop/",instance_id,token);


    }
    public static CloseableHttpResponse put(String url, String bodyEntity,String token) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        StringEntity entity = new StringEntity(bodyEntity, StandardCharsets.UTF_8);
        HttpPost httpPost = new HttpPost(url+bodyEntity);
        httpPost.addHeader("Authorization",token);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
        } catch (IOException e) {

        }
        return response;
    }
    public static String post(JSONObject json,String path){
            String result = "";
        try {
            HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(path);
                    post.setHeader("Content-Type","application/json");
                    //post.addHeader("Authorization","Basic YWRtaW46");
                    StringEntity s = new StringEntity(json.toString(),"utf-8");
                    s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
                    post.setEntity(s);
                    HttpResponse httpResponse = client.execute(post);
                    InputStream in = httpResponse.getEntity().getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in,"utf-8"));
                    StringBuilder strber = new StringBuilder();
                    String line = null;
                    while ((line = br.readLine())!=null){
                        strber.append(line+"\n");
                }
                    in.close();
                    result = strber.toString();
                    if (httpResponse.getStatusLine().getStatusCode()!= HttpStatus.SC_OK)
                    {
                         result = "服务器异常";
                    }

            }
            catch (Exception e){
                System.out.println("请求异常");
                throw new RuntimeException(e);

        }
        System.out.println("result == "+ result);
            return result;
    }

}